package gui;

import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import javax.swing.tree.DefaultTreeCellRenderer;
import bin.Armazem;
import bin.Persist.NotifyObserver;
import bin.err.Erro;
import gui.Persist.JanelaLoad;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

public class JanelaPrincipal extends javax.swing.JFrame implements NotifyObserver{

    //Atributos
    //private Integracao integracao;
    private JanelaFactory janelaFactory;
    private Armazem armazem;
    private File arquivoBD;
    
    //Construtores
    public JanelaPrincipal(File bd, Armazem armazem) {
        this.armazem = armazem;
        this.arquivoBD = bd;
        
        initComponents();
        
        this.setSysNames();
        this.setImagemTree();
        
        this.janelaFactory = new JanelaFactory(this);
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        menuTree = new javax.swing.JTree();
        desktopPane = new javax.swing.JDesktopPane();
        jLabel2 = new javax.swing.JLabel();
        lbVersion = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        menuSobre = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        menuTree.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("Menu");
        javax.swing.tree.DefaultMutableTreeNode treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("Faturamento");
        javax.swing.tree.DefaultMutableTreeNode treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Recebimento");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Venda");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("<ID>Pedido");
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("Cadastro");
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Cliente");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Item");
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("Pesquisa");
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Cliente");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("<ID>Item");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("<ID>Recebimento");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("<ID>Expedição");
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("Relatórios");
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Vendas por Cliente");
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("Cubos");
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Cubo Faturamento");
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("Estilo");
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Cor do Fundo");
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        menuTree.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        menuTree.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                menuTreeMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(menuTree);

        desktopPane.setBackground(new java.awt.Color(233, 255, 255));
        desktopPane.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/logoMarca.png"))); // NOI18N
        desktopPane.add(jLabel2);
        jLabel2.setBounds(10, 10, 200, 150);

        lbVersion.setFont(new java.awt.Font("Century Gothic", 0, 11)); // NOI18N
        lbVersion.setForeground(new java.awt.Color(0, 135, 180));
        lbVersion.setText("v1.0.0");
        desktopPane.add(lbVersion);
        lbVersion.setBounds(132, 157, 50, 14);

        jMenu1.setText("Opções");

        jMenuItem2.setText("Salvar");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Sobre");

        menuSobre.setText("Sobre");
        menuSobre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuSobreActionPerformed(evt);
            }
        });
        jMenu2.add(menuSobre);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(desktopPane, javax.swing.GroupLayout.DEFAULT_SIZE, 737, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 607, Short.MAX_VALUE)
            .addComponent(desktopPane)
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    //Métodos
    private void menuTreeMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_menuTreeMouseReleased
       
        JInternalFrame janela = this.janelaFactory.createJanela(this.menuTree.getSelectionPath() + "");
        System.out.println(this.menuTree.getSelectionPath() + "");
        if(janela != null)
        {
            this.desktopPane.add(janela);
            janela.setVisible(true);
        }
        this.menuTree.setSelectionRow(-1);
        
    }//GEN-LAST:event_menuTreeMouseReleased

    //Persistência
    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        
        JanelaLoad janelaLoad = new JanelaLoad(this, false, "Salvando registros em:", this.arquivoBD.getPath());

        try 
        {
            this.armazem.persistir(this.arquivoBD);
            JOptionPane.showMessageDialog(this, "Persistência efetuada com sucesso!", "Informação", JOptionPane.INFORMATION_MESSAGE);
        } 
        catch (IOException ex) 
        {
            new Erro(ex, "Problemas ao persistir no arquivo definido:\n" + this.arquivoBD.getPath(), false);
        }
        finally
        {
            janelaLoad.dispose();
        }
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void menuSobreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuSobreActionPerformed
        JanelaSobre janelaSobre = new JanelaSobre(this, true);
        janelaSobre.setVisible(true);
    }//GEN-LAST:event_menuSobreActionPerformed
    
    public void salvar() throws FileNotFoundException, IOException
    {
        FileOutputStream fos = new FileOutputStream(this.arquivoBD);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(this.getArmazem());
        oos.flush();
        oos.close();
    }
    
    //Setar nova imagem para Menu Arvore 
    private void setImagemTree()
    {
        DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer)menuTree.getCellRenderer();
        renderer.setClosedIcon(new ImageIcon("src\\res\\icons\\Pasta.png"));
        renderer.setOpenIcon(new ImageIcon("src\\res\\icons\\Pasta.png"));
        renderer.setLeafIcon(new ImageIcon("src\\res\\icons\\Janela.png"));
        this.menuTree.setCellRenderer(renderer);
    }
    
    //Setar nome e versão do sistema
    private void setSysNames()
    {
        this.setTitle(Integracao.NOME_SYS);
        this.lbVersion.setText(Integracao.VERSION);
    }
    
    //Get Desktop
    public JDesktopPane getDesktopPane()
    {
        return this.desktopPane;
    }

    //Get JanelaFactory
    public JanelaFactory getJanelaFactory()
    {
        return this.janelaFactory;
    }
    
    //Get Armazem (BD)
    public Armazem getArmazem()
    {
        return this.armazem;
    }
    
    @Override
    public void armazemNotify() {
        try 
        {
            this.armazem.persistir(this.arquivoBD);
        } 
        catch (IOException ex) 
        {
            new Erro(ex, "Problemas ao persistir no arquivo definido:\n" + this.arquivoBD.getPath(), false);
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDesktopPane desktopPane;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbVersion;
    private javax.swing.JMenuItem menuSobre;
    private javax.swing.JTree menuTree;
    // End of variables declaration//GEN-END:variables

}
